<template>
  <div>
    <!-- 操作 -->
    <div class="dis-o-between operate-line">
      <div>
        <el-button icon="el-icon-plus" plain type="primary" @click="add(0)" size="small">新增</el-button>
      </div>
      <RightToolbar :search="false" @queryTable="reqTableData"></RightToolbar>
    </div>
    <!-- 表格 -->
    <!-- default-expand-all -->
    <el-table :data="tableData" row-key="menuUuid" v-loading="loading" :tree-props="{ children: 'childMenu' }">
      <el-table-column prop="menuName" label="菜单/按钮名称"></el-table-column>
      <el-table-column v-for="(item, index) in tableTitle" :key="index" :prop="item.prop" :label="item.label"
        :type="item.type" :fixed="item.fixed ? item.fixed : null" :align="item.align" :width="item.width">
        <template slot-scope="scope">
          {{ item.prop === 'menuType' ? menuTypeFun(scope.row[item.prop]) : scope.row[item.prop] }}
        </template>
      </el-table-column>
      <el-table-column width="200" label="操作" align="center" size="small">
        <template slot-scope="scope">
          <el-button type="text" @click="edit(scope.row)">编辑</el-button>
          <el-button type="text" v-if="scope.row.menuType !== 4" @click="add(scope.row.menuType, scope.row)">
            {{ scope.row.menuType == 1 ? "添加二级菜单" : scope.row.menuType ==
              2 ? "添加三级菜单" : '添加按钮' }}
          </el-button>
          <el-button type="text" @click="Del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹框 -->
    <el-dialog v-dialogDrag :title="title" :visible.sync="dialogFormVisible" width="35%">
      <el-form :model="ruleForm" label-width="120px" :rules="rules" ref='formName'>
        <el-form-item label="菜单类型">
          <el-select v-model="ruleForm.menuType" disabled="" placeholder="请选择菜单类型">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单名称">
          <el-input v-model="ruleForm.menuName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="路由">
          <el-input v-model="ruleForm.route" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="ruleForm.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="ruleForm.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'menuVue',
  data() {
    return {
      loading: false,
      tableData: [],
      tableTitle: [
        { prop: 'menuType', label: '类型', width: '', align: 'center', fixed: "", type: "" },
        { prop: 'route', label: '路由', width: '', align: 'center', fixed: "", type: "" },
        { prop: 'icon', label: '图标', width: '', align: 'center', fixed: "", type: "" },
        { prop: 'sort', label: '排序', width: '80', align: 'center', fixed: "", type: "" },
      ],
      title: '',
      dialogFormVisible: false,
      rules: {
        sort: [{
          required: true,
          message: '请填写排序',
          trigger: 'blur'
        }]
      },
      ruleForm: {},
      options: [
        {
          value: 1,
          label: '一级菜单'
        }, {
          value: 2,
          label: '二级菜单'
        }, {
          value: 3,
          label: '三级菜单'
        }, {
          value: 4,
          label: '按钮'
        }
      ],
      menuType: 1,
    };
  },
  props: {},

  components: {},

  created() { },

  mounted() {
    this.reqTableData();
  },

  beforeDestroy() { },

  methods: {
    menuTypeFun(menuType) {
      const menuTypeMap = {
        1: '一级菜单',
        2: '二级菜单',
        3: '三级菜单',
        4: '按钮'
      }
      return menuTypeMap[menuType]
    },
    reqTableData() {
      this.loading = true;
      this.sendPost('/menu/list', {}).then(res => {
        if (res.errcode == 0) {
          this.tableData = res.data.voList || [];
          // console.log(this.tableData);
        } else {
          this.err({ message: res.errmsg })
        }
        this.$nextTick(() => {
          this.loading = false;
        })
      })
    },
    add(menuType, row) {
      console.log(row)
      this.title = menuType == 0 ? "添加一级菜单" : menuType == 1 ? "添加二级菜单" : menuType == 2 ? "添加三级菜单" : '添加按钮';
      this.ruleForm = {
        parentUuid: row ? row.menuUuid : null,
        menuType: menuType + 1,
        sort: menuType == 0 ? this.tableData.length + 1 : row.childMenu.length + 1
      }
      this.dialogFormVisible = true;
    },
    edit(row) {
      this.title = '编辑';
      this.ruleForm = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
    },
    submit() {
      this.$refs['formName'].validate((valid) => {
        if (valid) {
          this.sendPost('/menu/insertOrUpdateMenu', this.ruleForm).then((res) => {
            if (res.errcode == 0) {
              this.reqTableData();
              this.suc({ message: res.errmsg })
            } else {
              this.err({ message: res.errmsg })
            }
            this.dialogFormVisible = false;
          }).catch((error) => {
            console.log(error)
          })
        } else {
          return false;
        }
      })


    },
    Del(row) {
      this.$confirm('是否删除此菜单？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.sendGet('/menu/delete', { id: row.id }).then((res) => {
          if (res.errcode == 0) {
            this.suc({
              message: res.errmsg
            })
            this.reqTableData();
          } else {
            this.err({
              message: res.errmsg
            })
          }
        })
      })
    },
  },

  computed: {},

  watch: {},

  directives: {},

  filters: {}
};
</script>

<style scoped lang="scss"></style>
